package com.wzq.upms.core;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler;

import com.wzq.permission.client.model.admin.UserModel;
import com.wzq.upms.annotation.Auth;

/**
 * 权限拦截器
 *
 */
public class AuthInterceptor extends HandlerInterceptorAdapter {
	private final static Logger log= LogManager.getLogger(AuthInterceptor.class);

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object handler) throws Exception {
		if(handler instanceof DefaultServletHttpRequestHandler){
			return true;
		}
		String baseUri = request.getContextPath();
		HandlerMethod method = (HandlerMethod)handler;
		Auth  auth = method.getMethod().getAnnotation(Auth.class);
		ResponseBody  responseBody = method.getMethod().getAnnotation(ResponseBody.class);
		////验证登陆超时问题  auth = null，默认验证 
		if( auth == null || auth.verifyLogin()){
			UserModel user =SessionUtils.getUser(request);
			if(user  == null){
				if(responseBody!=null){
					response.setStatus(response.SC_GATEWAY_TIMEOUT);
					Map<String, Object> result = new HashMap<String, Object>();
					result.put(BaseController.SUCCESS, false);
					result.put(BaseController.LOGOUT_FLAG, true);//登录标记 true 退出
					result.put(BaseController.MSG, "登录超时.");
					HtmlUtil.writerJson(response, result);
					return false;
				}else{
					response.setStatus(response.SC_GATEWAY_TIMEOUT);
					response.sendRedirect(baseUri+"/loginPage");
					return false;
				}
			}
		}
		/*
		//验证URL权限
		if( auth == null || auth.verifyURL()){	
			//判断是否超级管理员
			if(!SessionUtils.isAdmin(request)){
				String url = request.getRequestURI();
				if(request.getQueryString()!=null){
					url+="?"+request.getQueryString();
				}
				String menuUrl = StringUtils.remove(url,request.getContextPath());
				if(!SessionUtils.isAccessUrl(request, StringUtils.trim(menuUrl))){			
					//日志记录
					String userName = SessionUtils.getUser(request).getUserName();
					String msg ="URL权限验证不通过:[url="+menuUrl+"][name ="+ userName+"]" ;
					log.error(msg);
					response.setStatus(response.SC_FORBIDDEN);
					if(responseBody!=null){
						JsonResponse jsonResponse = new JsonResponse();
						jsonResponse.setMsg("没有权限访问,请联系管理员.");
						jsonResponse.setResult(JsonResponse.FAIL);
						HtmlUtil.writerJson(response, jsonResponse);
						return false;
					}else{
						response.sendRedirect(baseUri+"/error/403");
						return false;
					}
				}
			}
		}*/
		return super.preHandle(request, response, handler);
	}

	
	
}
